package com.guigu.design;

/**
 * @description:
 * @author: lizhenhua05
 * @created: 2021/01/15 20:46
 * 单例模式
 */
public class Singleton {

    public static void main(String[] args) {
        Ehan ehan1 = Ehan.getEhan();
        Ehan ehan2 = Ehan.getEhan();
        System.out.println(ehan1.hashCode() == ehan2.hashCode());

        LanHan lanHan1 = LanHan.getLanHan();
        LanHan lanHan2 = LanHan.getLanHan();
        System.out.println(lanHan1.hashCode() == lanHan2.hashCode());

        LanHan2 instance2 = LanHan2.INSTANCE;
        LanHan2 instance1 = LanHan2.INSTANCE;

        System.out.println(instance1.hashCode() == instance2.hashCode());
    }
}

//饿汉模式
class Ehan{

    private Ehan(){

    }

    private static final Ehan ehan = new Ehan();

    public static Ehan getEhan(){
        return ehan;
    }
}

//懒汉模式，不推荐，效率不高
class  LanHan{
    private LanHan(){

    }

    private static LanHan lanHan;

    public static synchronized LanHan getLanHan(){
        if (lanHan == null){
            lanHan = new LanHan();
        }
        return lanHan;
    }

}

//懒汉模式
enum LanHan2{
    INSTANCE;
    public void sayOk(){
        System.out.println("ok");
    }
}